/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyControlRobotPanel.java
 *
 * Created on 8 déc. 2011, 11:16:12
 */
package my.monitor;

import java.awt.Point;
import java.beans.PropertyChangeEvent;
import my.monitor.controller.MyController;
import my.monitor.model.enumeration.MyDirection;
import my.monitor.model.enumeration.MyMode;
import my.monitor.model.enumeration.RobotMissionStatus;
import my.monitor.model.enumeration.RobotStatus;

/**
 *
 * @author Piro
 */
public class MyControlRobotPanel extends AbstractViewPanel {

    private MyController controller;

    public MyControlRobotPanel() {
        initComponents();
    }

    /** Creates new form MyControlRobotPanel */
    public MyControlRobotPanel(MyController controller) {
        this.controller = controller;
        initComponents();
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_DIRECTION)) {
            MyDirection newDirection = (MyDirection) evt.getNewValue();
            MyDirection oldDirection = (MyDirection) evt.getOldValue();
            if (newDirection == MyDirection.STOP) {
                jButtonStop.setEnabled(false);
            } else if (newDirection == MyDirection.BACKWARD) {
                jButtonBackward.setEnabled(false);
            } else if (newDirection == MyDirection.FORWARD) {
                jButtonForward.setEnabled(false);
            } else if (newDirection == MyDirection.LEFT) {
                jButtonLeft.setEnabled(false);
            } else if (newDirection == MyDirection.RIGHT) {
                jButtonRight.setEnabled(false);
            }
            if (oldDirection == MyDirection.STOP) {
                jButtonStop.setEnabled(true);
            } else if (oldDirection == MyDirection.BACKWARD) {
                jButtonBackward.setEnabled(true);
            } else if (oldDirection == MyDirection.FORWARD) {
                jButtonForward.setEnabled(true);
            } else if (oldDirection == MyDirection.LEFT) {
                jButtonLeft.setEnabled(true);
            } else if (oldDirection == MyDirection.RIGHT) {
                jButtonRight.setEnabled(true);
            }
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_STATUS)) {
            if ((RobotStatus) evt.getNewValue() == RobotStatus.CONNECTED) {
                setVisible(true);
                if (jTabbedPaneMode.getSelectedComponent() == jPanelManual) {
                    controller.changeMode(MyMode.MANUAL);
                } else {
                    controller.changeMode(MyMode.AUTOMATIC);
                }
            } else {
                setVisible(false);
                controller.changeMode(MyMode.NO_CONTROL);
            }
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_OBJECTIVE)) {
            Point objective = (Point) evt.getNewValue();
            if (objective.x != -1) {
                jTextFieldXCoordObjective.setText(String.valueOf((int) objective.x));
                jTextFieldYCoordObjective.setText(String.valueOf((int) objective.y));
                jButtonSendObjective.setEnabled(true);
            } else {
                jTextFieldXCoordObjective.setText("");
                jTextFieldYCoordObjective.setText("");
                jButtonSendObjective.setEnabled(false);
                jButtonStopMission.setEnabled(false);
            }
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_MISSION_STATUS)) {
            RobotMissionStatus status = (RobotMissionStatus) evt.getNewValue();
            if ((status == RobotMissionStatus.MISSION_RUNNING)
                    || (status == RobotMissionStatus.MISSION_STOP)){
                jButtonSendObjective.setEnabled(false);
                jButtonStopMission.setEnabled(true);
                jTabbedPaneMode.setEnabled(false);
            } else {
                jButtonSendObjective.setEnabled(true);
                jButtonStopMission.setEnabled(false);
                jTabbedPaneMode.setEnabled(true);
            }
            jTextFieldMissionState.setText(((RobotMissionStatus) evt.getNewValue()).name());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPaneMode = new javax.swing.JTabbedPane();
        jPanelManual = new javax.swing.JPanel();
        jButtonForward = new javax.swing.JButton();
        jButtonLeft = new javax.swing.JButton();
        jButtonRight = new javax.swing.JButton();
        jButtonBackward = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jSliderSpeed = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jPanelAutomatic = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldXCoordObjective = new javax.swing.JTextField();
        jTextFieldYCoordObjective = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButtonSendObjective = new javax.swing.JButton();
        jButtonStopMission = new javax.swing.JButton();
        jTextFieldMissionState = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(410, 340));
        setMinimumSize(new java.awt.Dimension(410, 340));
        setPreferredSize(new java.awt.Dimension(410, 340));

        jTabbedPaneMode.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPaneModeStateChanged(evt);
            }
        });

        jButtonForward.setText("Forward");
        jButtonForward.setPreferredSize(new java.awt.Dimension(97, 29));
        jButtonForward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonForwardActionPerformed(evt);
            }
        });

        jButtonLeft.setText("Left");
        jButtonLeft.setPreferredSize(new java.awt.Dimension(97, 29));
        jButtonLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLeftActionPerformed(evt);
            }
        });

        jButtonRight.setText("Right");
        jButtonRight.setPreferredSize(new java.awt.Dimension(97, 29));
        jButtonRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRightActionPerformed(evt);
            }
        });

        jButtonBackward.setText("Backward");
        jButtonBackward.setPreferredSize(new java.awt.Dimension(97, 29));
        jButtonBackward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackwardActionPerformed(evt);
            }
        });

        jButtonStop.setText("Stop");
        jButtonStop.setPreferredSize(new java.awt.Dimension(97, 29));
        jButtonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopActionPerformed(evt);
            }
        });

        jSliderSpeed.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderSpeedMouseReleased(evt);
            }
        });

        jLabel1.setText("Speed");

        org.jdesktop.layout.GroupLayout jPanelManualLayout = new org.jdesktop.layout.GroupLayout(jPanelManual);
        jPanelManual.setLayout(jPanelManualLayout);
        jPanelManualLayout.setHorizontalGroup(
            jPanelManualLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelManualLayout.createSequentialGroup()
                .add(jPanelManualLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelManualLayout.createSequentialGroup()
                        .add(29, 29, 29)
                        .add(jButtonLeft, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanelManualLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(jButtonForward, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButtonStop, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButtonBackward, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonRight, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanelManualLayout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(jLabel1)
                        .add(18, 18, 18)
                        .add(jSliderSpeed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanelManualLayout.setVerticalGroup(
            jPanelManualLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelManualLayout.createSequentialGroup()
                .add(38, 38, 38)
                .add(jButtonForward, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanelManualLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonStop, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButtonLeft, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButtonRight, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jButtonBackward, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(37, 37, 37)
                .add(jPanelManualLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(jLabel1)
                    .add(jSliderSpeed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        jTabbedPaneMode.addTab("Manual", jPanelManual);

        jLabel2.setText("Click on the screen to select an objective.");

        jLabel3.setText("X");

        jLabel4.setText("Y");

        jButtonSendObjective.setText("Send objective");
        jButtonSendObjective.setEnabled(false);
        jButtonSendObjective.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendObjectiveActionPerformed(evt);
            }
        });

        jButtonStopMission.setText("Stop mission");
        jButtonStopMission.setEnabled(false);
        jButtonStopMission.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopMissionActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelAutomaticLayout = new org.jdesktop.layout.GroupLayout(jPanelAutomatic);
        jPanelAutomatic.setLayout(jPanelAutomaticLayout);
        jPanelAutomaticLayout.setHorizontalGroup(
            jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelAutomaticLayout.createSequentialGroup()
                .add(jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jPanelAutomaticLayout.createSequentialGroup()
                            .addContainerGap()
                            .add(jLabel2))
                        .add(jPanelAutomaticLayout.createSequentialGroup()
                            .add(45, 45, 45)
                            .add(jLabel3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jTextFieldXCoordObjective, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jLabel4)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jTextFieldYCoordObjective, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jPanelAutomaticLayout.createSequentialGroup()
                        .add(101, 101, 101)
                        .add(jButtonSendObjective))
                    .add(jPanelAutomaticLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(jTextFieldMissionState, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(28, 28, 28)
                        .add(jButtonStopMission)))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        jPanelAutomaticLayout.setVerticalGroup(
            jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelAutomaticLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jTextFieldXCoordObjective, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jTextFieldYCoordObjective, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jLabel3)
                    .add(jLabel4))
                .add(18, 18, 18)
                .add(jButtonSendObjective)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelAutomaticLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextFieldMissionState, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButtonStopMission))
                .addContainerGap(129, Short.MAX_VALUE))
        );

        jTabbedPaneMode.addTab("Automatic", jPanelAutomatic);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPaneMode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPaneMode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRightActionPerformed
    controller.changeRobotDirection(MyDirection.RIGHT);
}//GEN-LAST:event_jButtonRightActionPerformed

private void jButtonLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLeftActionPerformed
    controller.changeRobotDirection(MyDirection.LEFT);

}//GEN-LAST:event_jButtonLeftActionPerformed

private void jButtonForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonForwardActionPerformed
    controller.changeRobotDirection(MyDirection.FORWARD);
}//GEN-LAST:event_jButtonForwardActionPerformed

private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStopActionPerformed
    controller.changeRobotDirection(MyDirection.STOP);
}//GEN-LAST:event_jButtonStopActionPerformed

private void jButtonBackwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackwardActionPerformed
    controller.changeRobotDirection(MyDirection.BACKWARD);
}//GEN-LAST:event_jButtonBackwardActionPerformed

private void jTabbedPaneModeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPaneModeStateChanged
    if (jTabbedPaneMode.getSelectedComponent() == jPanelManual) {
        controller.changeMode(MyMode.MANUAL);
    } else {
        controller.changeMode(MyMode.AUTOMATIC);
    }
}//GEN-LAST:event_jTabbedPaneModeStateChanged

private void jSliderSpeedMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderSpeedMouseReleased
    controller.changeRobotSpeed(new Integer(jSliderSpeed.getValue()));
}//GEN-LAST:event_jSliderSpeedMouseReleased

private void jButtonSendObjectiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSendObjectiveActionPerformed
    controller.changeRobotMissionStatus(RobotMissionStatus.MISSION_RUNNING);
}//GEN-LAST:event_jButtonSendObjectiveActionPerformed

private void jButtonStopMissionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStopMissionActionPerformed
    controller.changeRobotMissionStatus(RobotMissionStatus.MISSION_STOP);
}//GEN-LAST:event_jButtonStopMissionActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBackward;
    private javax.swing.JButton jButtonForward;
    private javax.swing.JButton jButtonLeft;
    private javax.swing.JButton jButtonRight;
    private javax.swing.JButton jButtonSendObjective;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JButton jButtonStopMission;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanelAutomatic;
    private javax.swing.JPanel jPanelManual;
    private javax.swing.JSlider jSliderSpeed;
    private javax.swing.JTabbedPane jTabbedPaneMode;
    private javax.swing.JTextField jTextFieldMissionState;
    private javax.swing.JTextField jTextFieldXCoordObjective;
    private javax.swing.JTextField jTextFieldYCoordObjective;
    // End of variables declaration//GEN-END:variables
}